package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.enums.workbench.MyTaskStatusEnum;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.workbench.AccountTask;
import com.xhwl.data.dao.IAccountDao;
import com.xhwl.data.dao.IAccountTaskDao;
import com.xhwl.data.service.IAccountTaskService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 16:02 2021/8/11
 */
@Service
public class AccountTaskServiceImpl extends ServiceImpl<IAccountTaskDao, AccountTask> implements IAccountTaskService {


    @Autowired
    private IAccountTaskDao accountTaskDao;

    @Autowired
    private IAccountDao accountDao;

    @Override
    public Map<Integer, List<Integer>> findByStatus(List<Integer> taskIds, MyTaskStatusEnum status) {

        Map<Integer, List<Integer>> map = new HashMap<>();
        QueryWrapper<AccountTask> accountTaskQueryWrapper = new QueryWrapper<>();
        accountTaskQueryWrapper.in("task_id", taskIds);
        accountTaskQueryWrapper.eq("status", status);
        List<AccountTask> accountTasks = accountTaskDao.selectList(accountTaskQueryWrapper);
        if (CollectionUtils.isNotEmpty(accountTasks)) {
            List<Integer> collect = accountTasks.stream().map(a -> a.getTaskId()).distinct().collect(Collectors.toList());
            for (Integer integer : collect) {
                List<Integer> collect1 = accountTasks.stream().filter(a -> a.getTaskId() == integer.intValue()).map(i -> i.getAccountId()).collect(Collectors.toList());
                map.put(integer, collect1);
            }
        }
        return map;
    }

    @Override
    public Map<Integer, String> findAccountNameByTaskId(List<Integer> taskIds, MyTaskStatusEnum status) {
        Map<Integer, String> map = new HashMap<>();

        QueryWrapper<AccountTask> accountTaskQueryWrapper = new QueryWrapper<>();
        accountTaskQueryWrapper.in("task_id", taskIds);
        accountTaskQueryWrapper.eq("status", status);
        List<AccountTask> accountTasks = accountTaskDao.selectList(accountTaskQueryWrapper);
        if (CollectionUtils.isNotEmpty(accountTasks)) {
            // 帐号id集合
            List<Integer> collect = accountTasks.stream().map(a -> a.getAccountId()).distinct().collect(Collectors.toList());

            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.select("name", "id");
            accountQueryWrapper.in("id", collect);
            List<Account> list = accountDao.selectList(accountQueryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                // key : accountId, value : name
                Map<Integer, String> collect3 = list.stream().collect(Collectors.toMap(Account::getId, Account::getName));
                for (Integer taskId : taskIds) {
                    List<Integer> collect2 = accountTasks.stream().filter(a -> a.getTaskId() == taskId.intValue()).map(i -> i.getAccountId()).collect(Collectors.toList());
                    StringBuilder sbd = new StringBuilder();
                    for (Integer integer1 : collect2) {
                        sbd.append(collect3.get(integer1)).append("、");
                    }
                    if (StringUtils.isNotEmpty(sbd)) {
                        String substring = sbd.substring(sbd.length() - 1, sbd.length());
                        String names = sbd.toString();
                        if ("、".equals(substring)) {
                            names = sbd.substring(0, sbd.length() - 1);
                        }
                        map.put(taskId, names);
                    }
                }
            }
        }

        return map;
    }
}
